home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / kill / RCS / kill.c,v < prev    next >
Encoding:
Text File  |  1990-11-11  |  10.6 KB  |  513 lines

  1. head     1.6;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.6
  10. date     90.11.11.12.31.59;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.5;
  13.  
  14. 1.5
  15. date     90.02.08.13.44.56;  author ouster;  state Exp;
  16. branches ;
  17. next     1.4;
  18.  
  19. 1.4
  20. date     88.10.21.17.29.54;  author ouster;  state Exp;
  21. branches ;
  22. next     1.3;
  23.  
  24. 1.3
  25. date     88.10.20.10.44.55;  author ouster;  state Exp;
  26. branches ;
  27. next     1.2;
  28.  
  29. 1.2
  30. date     88.10.20.10.07.48;  author ouster;  state Exp;
  31. branches ;
  32. next     1.1;
  33.  
  34. 1.1
  35. date     88.10.20.09.09.52;  author ouster;  state Exp;
  36. branches ;
  37. next     ;
  38.  
  39.  
  40. desc
  41. @@
  42.  
  43.  
  44. 1.6
  45. log
  46. @Recognize HUP.
  47. @
  48. text
  49. @/* 
  50.  * kill.c --
  51.  *
  52.  *    A program to send a signal to a process or process group.
  53.  *
  54.  * Copyright 1988 Regents of the University of California
  55.  * Permission to use, copy, modify, and distribute this
  56.  * software and its documentation for any purpose and without
  57.  * fee is hereby granted, provided that the above copyright
  58.  * notice appear in all copies.  The University of California
  59.  * makes no representations about the suitability of this
  60.  * software for any purpose.  It is provided "as is" without
  61.  * express or implied warranty.
  62.  */
  63.  
  64. #ifndef lint
  65. static char rcsid[] = "$Header: /sprite/src/cmds/kill/RCS/kill.c,v 1.5 90/02/08 13:44:56 ouster Exp Locker: kupfer $ SPRITE (Berkeley)";
  66. #endif not lint
  67.  
  68. #include <ctype.h>
  69. #include <errno.h>
  70. #include <option.h>
  71. #include <signal.h>
  72. #include <stdio.h>
  73. #include <stdlib.h>
  74. #include <string.h>
  75.  
  76. /*
  77.  * Variables and tables used to parse command-line options:
  78.  */
  79.  
  80. int sendToGroup = 0;
  81. int printSignals = 0;
  82. int signalNumber = SIGTERM;
  83.  
  84. Option optionArray[] = {
  85.     {OPT_DOC, (char *) NULL, (char *) NULL,
  86.         "kill [-sig] [options] pid pid ..."},
  87.     {OPT_DOC, (char *) NULL, (char *) NULL,
  88.         "\"Sig\" may be either a signal name or a number (default: TERM).\n"},
  89.     {OPT_TRUE, "g", (char *) &sendToGroup,
  90.         "Send signal to group instead of single process"},
  91.     {OPT_TRUE, "l", (char *) &printSignals,
  92.         "Print out list of valid signal names"}
  93. };
  94.  
  95. /*
  96.  * Printable names and identifying messages for all signals, indexed by
  97.  * signal number.
  98.  */
  99.  
  100. struct    info {
  101.     char    *name;
  102.     char    *reason;
  103. } info[] = {
  104.     0, 0,
  105.     "HUP",        "Hangup",
  106.     "INT",        "Interrupt",
  107.     "DEBUG",    "Debug",
  108.     "ILL",        "Illegal instruction",
  109.     0,        "Signal 5",
  110.     "IOT",        "IOT instruction",
  111.     "EMT",        "EMT instruction",
  112.     "FPE",        "Floating-point exception",
  113.     "KILL",        "Killed",
  114.     "MIG",        "Migrated",
  115.     "SEGV",        "Segmentation violation",
  116.     0,        "Signal 12",
  117.     "PIPE",        "Broken pipe",
  118.     "ALRM",        "Alarm clock",
  119.     "TERM",        "Software termination",
  120.     "URG",        "Urgent I/O condition",
  121.     "STOP",        "Suspended (signal)",
  122.     "TSTP",        "Suspended",
  123.     "CONT",        "Continued",
  124.     "CHLD",        "Child status changed",
  125.     "TTIN",        "Suspended (tty input)",
  126.     0,        "Signal 22",
  127.     "IO",        "I/O is possible on a descriptor",
  128.     0,        "Signal 24",
  129.     0,        "Signal 25",
  130.     0,        "Signal 26",
  131.     0,        "Signal 27",
  132.     "WINCH",    "Window changed",
  133.     "MIGHOME",    "Migrated home",
  134.     "USR1",        "User-defined signal 1",
  135.     "USR2",        "User-defined signal 2",
  136.     0,        "Signal 32",
  137. };
  138.  
  139. /*
  140.  *----------------------------------------------------------------------
  141.  *
  142.  * main --
  143.  *
  144.  *    Main program for "kill".
  145.  *
  146.  * Results:
  147.  *    None.
  148.  *
  149.  * Side effects:
  150.  *    See the man page for details.
  151.  *
  152.  *----------------------------------------------------------------------
  153.  */
  154.  
  155. main(argc, argv)
  156.     int argc;
  157.     char **argv;
  158. {
  159.     int i, j, newArgc;
  160.     int result = 0;
  161.     char *sigName, *endPtr;
  162.     char scratch[10];
  163.  
  164.     /*
  165.      * Make a pass over the options to suck up the signal number or name.
  166.      * Then call Opt_ to parse the remaining options.
  167.      */
  168.  
  169.     for (i = 1, newArgc = 1; i < argc; i++) {
  170.     char *p;
  171.  
  172.     p = argv[i];
  173.     if (*p != '-') {
  174.         argv[newArgc] = argv[i];
  175.         newArgc++;
  176.         continue;
  177.     }
  178.     p++;
  179.     if (isdigit(*p)) {
  180.         signalNumber = strtoul(p, &endPtr, 0);
  181.         if (*endPtr != 0) {
  182.         (void) fprintf(stderr, "Bad signal number \"%s\".\n", p);
  183.         exit(1);
  184.         }
  185.     } else {
  186.         for (j = 1; j <= 32; j++) {
  187.         if ((info[j].name != 0) && (strcmp(p, info[j].name) == 0)) {
  188.             signalNumber = j;
  189.             break;
  190.         }
  191.  
  192.         /*
  193.          * If this switch didn't match a signal name, save the
  194.          * argument for Opt_ processing.
  195.          */
  196.  
  197.         if (j == 32) {
  198.             argv[newArgc] = argv[i];
  199.             newArgc++;
  200.         }
  201.         }
  202.     }
  203.     }
  204.     argc = Opt_Parse(newArgc, argv, optionArray, Opt_Number(optionArray), 0);
  205.  
  206.     /*
  207.      * If the user just wants a list of signals, print it and quit.
  208.      */
  209.  
  210.     if (printSignals) {
  211.     for (i = 0; i < 32; i++) {
  212.         if (info[i].name == 0) {
  213.         continue;
  214.         }
  215.         (void) printf("%2d  %-15s %s\n", i, info[i].name, info[i].reason);
  216.     }
  217.     exit(0);
  218.     }
  219.  
  220.     if (argc == 1) {
  221.     (void) fprintf(stderr, "Usage:  %s [options] pid pid ...\n", argv[0]);
  222.     exit(1);
  223.     }
  224.  
  225.     if (signalNumber <= 32) {
  226.     sigName = info[signalNumber].name;
  227.     } else {
  228.     (void) sprintf(scratch, "Signal %d", signalNumber);
  229.     sigName = scratch;
  230.     }
  231.  
  232.     for (i = 1; i < argc; i++) {
  233.     int pid;
  234.     char *endPtr;
  235.  
  236.     pid = strtoul(argv[i], &endPtr, 16);
  237.     if ((endPtr == argv[i]) || (*endPtr != 0)) {
  238.         (void) fprintf(stderr, "Bad process id \"%s\";  ignoring.\n", argv[i]);
  239.         continue;
  240.     }
  241.  
  242.     if (sendToGroup) {
  243.         if (killpg(pid, signalNumber) != 0) {
  244.             (void) fprintf(stderr,
  245.                 "Couldn't send %s signal to group 0x%x: %s.\n",
  246.                 sigName, pid, strerror(errno));
  247.         }
  248.         result = 1;
  249.     } else {
  250.         if (kill(pid, signalNumber) != 0) {
  251.             (void) fprintf(stderr,
  252.                 "Couldn't send %s signal to process 0x%x: %s.\n",
  253.                 sigName, pid, strerror(errno));
  254.         }
  255.         result = 1;
  256.     }
  257.     }
  258.     return result;
  259. }
  260. @
  261.  
  262.  
  263. 1.5
  264. log
  265. @Added support for SIGWINCH and SIGIO.
  266. @
  267. text
  268. @d17 1
  269. a17 1
  270. static char rcsid[] = "$Header: /sprite/src/cmds/kill/RCS/kill.c,v 1.4 88/10/21 17:29:54 ouster Exp Locker: ouster $ SPRITE (Berkeley)";
  271. d57 1
  272. a57 1
  273.     0,        "Signal 1",
  274. @
  275.  
  276.  
  277. 1.4
  278. log
  279. @Switch to use same messages as csh.
  280. @
  281. text
  282. @d17 1
  283. a17 1
  284. static char rcsid[] = "$Header: /a/newcmds/kill/RCS/kill.c,v 1.3 88/10/20 10:44:55 ouster Exp $ SPRITE (Berkeley)";
  285. d79 1
  286. a79 1
  287.     0,        "Signal 23",
  288. d84 1
  289. a84 1
  290.     0,        "Signal 28",
  291. @
  292.  
  293.  
  294. 1.3
  295. log
  296. @Lint cleanup.
  297. @
  298. text
  299. @d17 1
  300. a17 1
  301. static char rcsid[] = "$Header: /a/newcmds/kill/RCS/kill.c,v 1.2 88/10/20 10:07:48 ouster Exp Locker: ouster $ SPRITE (Berkeley)";
  302. d52 37
  303. a88 34
  304. char *names[] = {
  305.     "0",
  306.     "1",
  307.     "INT",
  308.     "DEBUG",
  309.     "ILL",
  310.     "5",
  311.     "IOT",
  312.     "EMT",
  313.     "FPE",
  314.     "KILL",
  315.     "10",
  316.     "SEGV",
  317.     "12",
  318.     "PIPE",
  319.     "ALRM",
  320.     "TERM",
  321.     "URG",
  322.     "STOP",
  323.     "TSTP",
  324.     "CONT",
  325.     "CHLD",
  326.     "TTIN",
  327.     "22",
  328.     "23",
  329.     "24",
  330.     "25",
  331.     "26",
  332.     "27",
  333.     "28",
  334.     "29",
  335.     "30",
  336.     "31",
  337.     "32"
  338. a89 35
  339. char *messages[] = {
  340.     NULL,
  341.     NULL,
  342.     "Interrupt",
  343.     "Stop process for debugging",
  344.     "Illegal instruction",
  345.     NULL,
  346.     "IOT instruction",
  347.     "EMT instruction",
  348.     "Floating-point exception",
  349.     "Kill (cannot be caught or blocked or ignored)",
  350.     NULL,
  351.     "Segmentation violation",
  352.     NULL,
  353.     "Write on reader-less pipe",
  354.     "Alarm clock",
  355.     "Software termination signal",
  356.     "Urgent condition present on socket",
  357.     "Stop (cannot be caught or blocked or ignored)",
  358.     "Stop signal generated from keyboard",
  359.     "Continue (cannot be blocked)",
  360.     "Child status has changed",
  361.     "Background read attempted from control terminal",
  362.     NULL,
  363.     NULL,
  364.     NULL,
  365.     NULL,
  366.     NULL,
  367.     NULL,
  368.     NULL,
  369.     NULL,
  370.     NULL,
  371.     NULL,
  372.     NULL
  373. };
  374. d139 1
  375. a139 1
  376.         if (strcmp(p, names[j]) == 0) {
  377. d164 1
  378. a164 1
  379.         if (messages[i] == NULL) {
  380. d167 1
  381. a167 1
  382.         (void) printf("%2d  %-8s %s\n", i, names[i], messages[i]);
  383. d178 1
  384. a178 1
  385.     sigName = names[signalNumber];
  386. d180 1
  387. a180 1
  388.     (void) sprintf(scratch, "%d", signalNumber);
  389. @
  390.  
  391.  
  392. 1.2
  393. log
  394. @First version up and running.
  395. @
  396. text
  397. @d17 1
  398. a17 1
  399. static char rcsid[] = "$Header: proto.c,v 1.2 88/03/11 08:39:08 ouster Exp $ SPRITE (Berkeley)";
  400. d25 1
  401. d166 1
  402. a166 1
  403.         fprintf(stderr, "Bad signal number \"%s\".\n", p);
  404. d199 1
  405. a199 1
  406.         printf("%2d  %-8s %s\n", i, names[i], messages[i]);
  407. d205 1
  408. a205 1
  409.     fprintf(stderr, "Usage:  %s [options] pid pid ...\n", argv[0]);
  410. d212 1
  411. a212 1
  412.     sprintf(scratch, "%d", signalNumber);
  413. d217 1
  414. a217 1
  415.     int pid, status;
  416. d222 1
  417. a222 1
  418.         fprintf(stderr, "Bad process id \"%s\";  ignoring.\n", argv[i]);
  419. d228 1
  420. a228 1
  421.             fprintf(stderr,
  422. d235 1
  423. a235 1
  424.             fprintf(stderr,
  425. @
  426.  
  427.  
  428. 1.1
  429. log
  430. @Initial revision
  431. @
  432. text
  433. @d36 4
  434. d43 1
  435. a43 32
  436.         "Print out list of valid signal names"},
  437.     {OPT_CONSTANT(SIGINT), "INT", (char *) &signalNumber, "Interrupt"},
  438.     {OPT_CONSTANT(SIGQUIT), "QUIT", (char *) &signalNumber, "Quit"},
  439.     {OPT_CONSTANT(SIGILL), "ILL", (char *) &signalNumber,
  440.         "Illegal instruction"},
  441.     {OPT_CONSTANT(SIGIOT), "IOT", (char *) &signalNumber, "IOT instruction"},
  442.     {OPT_CONSTANT(SIGEMT), "EMT", (char *) &signalNumber, "EMT instruction"},
  443.     {OPT_CONSTANT(SIGFPE), "FPE", (char *) &signalNumber,
  444.         "Floating-point exception"},
  445.     {OPT_CONSTANT(SIGKILL), "KILL", (char *) &signalNumber,
  446.         "Kill (cannot be caught or blocked or ignored)"},
  447.     {OPT_CONSTANT(SIGSEGV), "SEGV", (char *) &signalNumber,
  448.         "Segmentation violation"},
  449.     {OPT_CONSTANT(SIGPIPE), "PIPE", (char *) &signalNumber,
  450.         "Write on reader-less pipe"},
  451.     {OPT_CONSTANT(SIGALRM), "ALRM", (char *) &signalNumber, "Alarm clock"},
  452.     {OPT_CONSTANT(SIGTERM), "TERM", (char *) &signalNumber,
  453.         "Software termination signal"},
  454.     {OPT_CONSTANT(SIGURG), "URG", (char *) &signalNumber,
  455.         "Urgent condition present on socket"},
  456.     {OPT_CONSTANT(SIGSTOP), "STOP", (char *) &signalNumber,
  457.         "Stop (cannot be caught or blocked or ignored)"},
  458.     {OPT_CONSTANT(SIGTSTP), "TSTP", (char *) &signalNumber,
  459.         "Stop signal generated from keyboard"},
  460.     {OPT_CONSTANT(SIGCONT), "CONT", (char *) &signalNumber,
  461.         "Continue (cannot be blocked)"},
  462.     {OPT_CONSTANT(SIGCHLD), "CHLD", (char *) &signalNumber,
  463.         "Child status has changed"},
  464.     {OPT_CONSTANT(SIGTTIN), "TTIN", (char *) &signalNumber,
  465.         "Background read attempted from control terminal"},
  466.     {OPT_CONSTANT(SIGQUIT), "DEBUG", (char *) &signalNumber,
  467.         "Stop process for debugging"},
  468. d87 2
  469. a88 2
  470.     "Signal 0 (undefined)",
  471.     "Signal 1 (undefined)",
  472. d92 1
  473. a92 1
  474.     "Signal 5 (undefined)",
  475. d97 1
  476. a97 1
  477.     "Signal 10 (undefined)",
  478. d99 1
  479. a99 1
  480.     "Signal 12 (undefined)",
  481. d109 11
  482. d142 1
  483. a142 1
  484.     int i;
  485. d144 1
  486. a144 1
  487.     char *sigName;
  488. d148 1
  489. a148 1
  490.      * Make a pass over the options to suck up the signal name.
  491. d152 1
  492. a152 1
  493.     for (i = 1; i < argc; i++) {
  494. d157 2
  495. d161 13
  496. a173 4
  497.     if (isdigit(
  498.     /*
  499.      * Suck up command line options.
  500.      */
  501. d175 11
  502. a185 4
  503.     argc = Opt_Parse(argc, argv, optionArray, Opt_Number(optionArray), 0);
  504.     if (argc == 1) {
  505.     fprintf(stderr, "Usage:  %s [options] pid pid ...\n", argv[0]);
  506.     return 1;
  507. d187 1
  508. d194 5
  509. a198 1
  510.     for (i = 0; i < Opt_Number(optionArray); i++) {
  511. d201 5
  512. @
  513.